<?php

/*
 * Classe Setting: serve per caricare le impostazioni del forum
 * 
 */

/**
 * Description of Setting
 *
 * @author Stefano Poncini
 */
class Setting {
    public $post_page;
    public $forum_page;
    public $language;
    
    /**
     * Costruttore della classe setting: carica le impostazioni dal database
     */
    public function __construct(){
        
        $sql_setting = "select *, count(*) as num from setting";

        $stmt = DB::getDBO()->prepare($sql_setting);
        $stmt->execute();
                

        $result = $stmt->fetch();

        if ($result['num'] != 1) {
            KLogger::instance("log", KLogger::DEBUG)->logError("Errore durante il caricamento delle impostazioni");

        } else {
            KLogger::instance("log", KLogger::DEBUG)->logInfo("Setting caricate con successo");

            $this->post_page = $result['post_page']; 
            $this->forum_page = $result['forum_page']; 
            $this->language = $result['language'];
        }
    }
    
    /**
     * Metodo updateSettings: metodo che salva le impostazioni
     * 
     * @param string $language
     * @param int $post_page
     * @param int $forum_page
     * @static
     * @return boolean
     */
    public static function updateSettings($language, $post_page, $forum_page){
        
        try {
            $sql = "UPDATE setting SET post_page = ?, forum_page = ?, language = ?";
            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array($post_page, $forum_page, $language));
            return true;
        } catch (PDOException $e) {

            return false;
        }
    }
    
}


?>
